/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "scan_callback_proxy.h"

#include "message_parcel.h"
#include "scan_log.h"

namespace OHOS::Scan {
ScanCallbackProxy::ScanCallbackProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IScanCallback>(impl) {}


bool ScanCallbackProxy::OnCallback(uint32_t state, const ScanDeviceInfoTCP &info)
{
    SCAN_HILOGE("GetScannerList ScanCallbackProxy::OnCallback");
    SCAN_HILOGD("ScanCallbackProxy::OnCallback Start");
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(GetDescriptor())) {
        SCAN_HILOGE("write descriptor failed");
        return false;
    }

    data.WriteUint32(state);
    info.Marshalling(data);

    int error = Remote()->SendRequest(SCAN_CALLBACK_DEVICE_TCP, data, reply, option);
    if (error != 0) {
        SCAN_HILOGE("SendRequest failed, error %{public}d", error);
        return false;
    }
    
    SCAN_HILOGD("ScanCallbackProxy::OnCallback End");
    return true;
}

bool ScanCallbackProxy::OnCallback(uint32_t state, const ScanDeviceInfoLibUSB &info)
{
    SCAN_HILOGE("GetScannerList ScanCallbackProxy::OnCallback");
    SCAN_HILOGD("ScanCallbackProxy::OnCallback Start");
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(GetDescriptor())) {
        SCAN_HILOGE("write descriptor failed");
        return false;
    }

    data.WriteUint32(state);
    info.Marshalling(data);

    int error = Remote()->SendRequest(SCAN_CALLBACK_DEVICE_LIBUSB, data, reply, option);
    if (error != 0) {
        SCAN_HILOGE("SendRequest failed, error %{public}d", error);
        return false;
    }
    
    SCAN_HILOGD("ScanCallbackProxy::OnCallback End");
    return true;
}

bool ScanCallbackProxy::OnGetFrameResCallback(bool isGetSucc, int32_t sizeRead)
{
    SCAN_HILOGD("ScanCallbackProxy::OnGetFrameResCallback Start");
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(GetDescriptor())) {
        SCAN_HILOGE("write descriptor failed");
        return false;
    }

    data.WriteBool(isGetSucc);
    data.WriteInt32(sizeRead);

    int error = Remote()->SendRequest(SCAN_CALLBACK_GET_FRAME_RES, data, reply, option);
    if (error != 0) {
        SCAN_HILOGE("SendRequest failed, error %{public}d", error);
        return false;
    }
    SCAN_HILOGD("ScanCallbackProxy::OnGetFrameResCallback End");
    return true;
}

bool ScanCallbackProxy::OnScanInitCallback(int32_t &scanVersion)
{
    SCAN_HILOGE("Enter OnScanInitCallback");
    SCAN_HILOGD("ScanCallbackProxy::OnCallback Start");
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(GetDescriptor())) {
        SCAN_HILOGE("write descriptor failed");
        return false;
    }

    data.WriteUint32(scanVersion);

    int error = Remote()->SendRequest(SCAN_CALLBACK_SCAN_INIT, data, reply, option);
    if (error != 0) {
        SCAN_HILOGE("SendRequest failed, error %{public}d", error);
        return false;
    }
    
    SCAN_HILOGD("ScanCallbackProxy::OnCallback End");
    return true;
}

} // namespace OHOS::Scan
